#!/bin/bash
# source ~/.config/scripts/fzf-sshscp/servers.sh

PROJECT_ROOT="$HOME/.config/scripts/fzf-sshscp"
# readonly PROJECT_ROOT=$(dirname `readlink --canonicalize "${BASH_SOURCE[0]}"`)
# readonly DIR_CONFIG="$HOME/.config/fmui"
# source "$PROJECT_ROOT/servers.sh"

si=0
set_server() {
    servers[$si]="$1"
    sshcmds[$si]="$2"
    scpcmds[$si]="$3"
    targets[$si]="$4"
    copytext[$si]="$5"
    # sshcmdstr[$si]="$1 $2"
    # scpcmdstr[$si]="$2 $2"
    si=$(($si+1))
}

# set_server name cmd target(user@host) host
# set_server '1.demo' 'ssh'                        'root@demo'        'demo'
# set_server '2.121.4.29.84' 'ssh -R 2489:127.0.0.1:2489' 'root@121.4.29.84' '121.4.29.84'
# source ./databases.sh
# source ./databases-example.sh
# [ -f ./databases.sh ] && source ./databases.sh
# if [[ -f ~/.config/scripts/fzf-sshscp/databases.sh ]]; then
#     source ~/.config/scripts/fzf-sshscp/databases.sh
# else
#     cp ~/.config/scripts/fzf-sshscp/databases-example.sh ~/.config/scripts/fzf-sshscp/databases.sh 
#     # [ -f ./databases.sh ] && source ~/.config/scripts/fzf-sshscp/databases.sh
#     source ~/.config/scripts/fzf-sshscp/databases-example.sh
# fi
if [[ -f "$PROJECT_ROOT/databases.sh" ]]; then
    source "$PROJECT_ROOT/databases.sh"
else
    cp "$PROJECT_ROOT/databases-example.sh" "$PROJECT_ROOT/databases.sh"
    # [ -f ./databases.sh ] && source ~/.config/scripts/fzf-sshscp/databases.sh
    source "$PROJECT_ROOT/databases-example.sh"
fi


_list() {
    for ((i = 0; i < ${#servers[@]}; i++)); do
        _item=${servers[$i]}
        echo "${_item[@]}"
    done
}


_index() {
    for ((i = 0; i < ${#servers[@]}; i++)); do
        _item=${servers[$i]}
        [[ "$_item" == "$1" ]] && echo $i && break
    done
}
